package com.caogen.cg_wl_kt.ui.fragment.home

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.caogen.cg_wl_kt.adapter.HomeAdapter
import com.caogen.cg_wl_kt.base.Base2Adapter
import com.caogen.cg_wl_kt.base.BaseFragment
import com.caogen.cg_wl_kt.databinding.FragmentHomeBinding
import com.caogen.cg_wl_kt.model.home.HomeListBean
import com.caogen.cg_wl_kt.utils.YDLog
import com.caogen.cg_wl_kt.vmodel.fragment.HomeViewModel
import com.scwang.smart.refresh.footer.ClassicsFooter
import com.scwang.smart.refresh.header.ClassicsHeader

class HomeFragment : BaseFragment<FragmentHomeBinding,HomeViewModel>() {
    lateinit var adapter: HomeAdapter
    override fun initView(view: View?) {
        super.initView(view)
        vm.activity = this
        adapter = HomeAdapter(this.context());
        binding.rcView.adapter = adapter
        binding.refreshLayout.setRefreshHeader(ClassicsHeader(context()))
        binding.refreshLayout.setRefreshFooter(ClassicsFooter(context()))
        vm.getListData()
    }

    override fun setListener() {
        super.setListener()
        binding.refreshLayout.setOnRefreshListener {
            vm.page = 1
            vm.getListData()
        }
        binding.refreshLayout.setOnLoadMoreListener {
            vm.page++
            vm.getListData()//请求数据的方法
        }
        adapter.mItemClickListener = object :Base2Adapter.OnItemClickListener<HomeListBean>{
            override fun onItemClick(item: HomeListBean, position: Int) {
                YDLog.logd(item.id.toString()+"----"+position)
            }
        }
    }
    override fun vmClass(): Class<HomeViewModel> {
        return HomeViewModel::class.java
    }
    override fun binding(inflater: LayoutInflater, container: ViewGroup?): FragmentHomeBinding {
        return FragmentHomeBinding.inflate(layoutInflater)
    }

}